package org.jaaksi.demo.ptr.ptr.header;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;
import org.jaaksi.demo.ptr.R;
import org.jaaksi.demo.ptr.widegit.RoundProgressBar;

/**
 * Created by fcy on 2015/1/20. <br/>
 * 可以自定义炫酷的loading动画，如loading过程中显示逐帧动画
 * 调用autoRefresh时没有status=2的状态（PTR_STATUS_PREPARE 预备态） 调用顺序：onUIRefreshPrepare -
 * onUIRefreshBegin - onUIPositionChange.status=3 - onUIRefreshComplete -
 * onUIReset - onUIPositionChange.status=1
 */
public class TaoBaoHeader extends FrameLayout implements PtrUIHandler {
  /**
   * <li/>这个值根据progressbar的空隙角度决定max=360时，
   * <li/>这个指=空隙角度，要控制好progressbar的起始角度
   */
  private final int MAX_PROGRESS = 340;
  private LayoutInflater inflater;
  // 下拉刷新视图（头部视图）
  private ViewGroup headView;
  // 下拉刷新文字
  private TextView tvHeadTitle;
  private View loadingBar;
  private RoundProgressBar rpb;
  // 中心箭头
  private ImageView ivArrow;

  public TaoBaoHeader(Context context) {
    this(context, null);
  }

  public TaoBaoHeader(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public TaoBaoHeader(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
  }

  private void init(Context context) {
    inflater = LayoutInflater.from(context);
    headView = (ViewGroup) inflater.inflate(R.layout.cus_header_taobao, this, true);
    loadingBar = headView.findViewById(R.id.loading_bar);
    rpb = (RoundProgressBar) loadingBar.findViewById(R.id.rpb);
    ivArrow = (ImageView) loadingBar.findViewById(R.id.iv_arrow);
    tvHeadTitle = (TextView) headView.findViewById(R.id.tv_head_title);
  }

  /**
   * Content 重新回到顶部， Header 消失，整个下拉刷新过程完全结束以后（只调一次）
   * 随后立刻调一次onUIPositionChange,status=PTR_STATUS_INIT,重置 View
   */
  @Override public void onUIReset(PtrFrameLayout ptrFrameLayout) {
    tvHeadTitle.setText("下拉刷新");
  }

  /**
   * Header 将要出现时调用（只调一次）
   */
  @Override public void onUIRefreshPrepare(PtrFrameLayout ptrFrameLayout) {
    tvHeadTitle.setText("下拉刷新");
  }

  /**
   * 开始刷新，Header 进入刷新状态之前调用（只调用一次）
   */
  @Override public void onUIRefreshBegin(PtrFrameLayout ptrFrameLayout) {
    tvHeadTitle.setText("正在刷新");
    hideArrow();
    Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_rotate_arror);
    loadingBar.startAnimation(animation);
  }

  /**
   * 刷新结束，Header 开始向上移动之前调用（只调一次）
   */
  @Override public void onUIRefreshComplete(PtrFrameLayout ptrFrameLayout) {
    loadingBar.clearAnimation();
    tvHeadTitle.setText("刷新完成");
  }

  public void hideArrow() {
    ivArrow.setVisibility(GONE);
    rpb.setProgress(MAX_PROGRESS);
  }

  /**
   * <li/>移动过程中位置变化回调 PTR_STATUS_PREPARE = 2;
   * <li/>刷新前的状态，与位置无关 PTR_STATUS_LOADING = 3;
   * <li/>正在刷新 PTR_STATUS_COMPLETE = 4;
   */
  @Override public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status,
      PtrIndicator ptrIndicator) {
    int mOffsetToRefresh = frame.getOffsetToRefresh();
    int currentPos = ptrIndicator.getCurrentPosY();
    int lastPos = ptrIndicator.getLastPosY();
    if (status == PtrFrameLayout.PTR_STATUS_PREPARE) {
      int min = (int) Math.min(360f * currentPos / mOffsetToRefresh, MAX_PROGRESS);
      if (currentPos < mOffsetToRefresh) {
        ivArrow.setVisibility(VISIBLE);
        rpb.setProgress(min);
      } else {
        hideArrow();
      }
    }
    // else if (status == PtrFrameLayout.PTR_STATUS_LOADING)
    // {
    // hideArrow();
    // }

    if (currentPos < mOffsetToRefresh && lastPos >= mOffsetToRefresh) {
      if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) { // 临界点上移
        tvHeadTitle.setText("下拉刷新");
      }
    } else if (currentPos > mOffsetToRefresh && lastPos <= mOffsetToRefresh) { // 超过临界点，继续下拉
      if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
        tvHeadTitle.setText("松开刷新");
      }
    }
  }
}
